﻿using IServices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using YL.Core.Entity;

namespace AliIotAmqpWorker
{
    public class DeviceDataService
    {
        private readonly IMeter_DeviceDataServices deviceDataServices;
        private readonly IMeter_DeviceDataHistoryServices deviceDataHistoryServices;
        private readonly IMeter_ClientInfoServices clientInfoServices;
        private readonly IMeter_DeviceInfoServices deviceInfoServices;

        public DeviceDataService(IMeter_DeviceDataServices DeviceDataServices,
                                 IMeter_DeviceDataHistoryServices DeviceDataHistoryServices,
                                 IMeter_ClientInfoServices ClientInfoServices,
                                 IMeter_DeviceInfoServices DeviceInfoServices)
        {
            deviceDataServices = DeviceDataServices;
            deviceDataHistoryServices = DeviceDataHistoryServices;
            clientInfoServices = ClientInfoServices;
            deviceInfoServices = DeviceInfoServices;
        }
        /// <summary>
        /// 获取当前数据
        /// </summary>
        /// <param name="rmd_MeterProperty"></param>
        /// <returns></returns>
        public List<EnumStruct> GetPropertyDatas(ECU1151Info deviceData)
        {
            List<EnumStruct> list = new List<EnumStruct>();
            //list.Add(new EnumStruct() { Key = "Pump1Alarm", Value = deviceData.Pump1Alarm.value, Description = "泵1故障" });
            //list.Add(new EnumStruct() { Key = "Pump1localremote", Value = deviceData.Pump1LocalRemote.value, Description = "泵1本地远程" });
            //list.Add(new EnumStruct() { Key = "Pump1Run", Value = deviceData.Pump1Run.value, Description = "泵1运行" });
            //list.Add(new EnumStruct() { Key = "Pump2Alarm", Value = deviceData.Pump2Alarm.value, Description = "泵2故障" });
            //list.Add(new EnumStruct() { Key = "Pump2LocalRemote", Value = deviceData.Pump2LocalRemote.value, Description = "泵2本地远程" });
            //list.Add(new EnumStruct() { Key = "Pump2Run", Value = deviceData.Pump2Run.value, Description = "泵2运行" });
            //list.Add(new EnumStruct() { Key = "WaterLevel", Value = deviceData.WaterLevel.value, Description = "液位" });
            //list.Add(new EnumStruct() { Key = "WaterPump1boot", Value = deviceData.WaterPump1boot.value, Description = "水泵1运行"});
            //list.Add(new EnumStruct() { Key = "WaterPump2Run", Value = deviceData.WaterPump2Run.value, Description = "水泵2运行" });
            Type type = deviceData.GetType();
            var properties = typeof(ECU1151Info).GetProperties();
            foreach (var pi in properties)
            {
                EnumStruct enumStruct = new EnumStruct();
                enumStruct.Key= pi.Name;

                AttributeCollection attributes = TypeDescriptor.GetProperties(type)[pi.Name].Attributes;
                DescriptionAttribute myAttribute = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
                enumStruct.Description = myAttribute.Description;

                object value = pi.GetValue(deviceData, null);
                if(value!=null)
                {
                    PropertyValue<double> pvalue = (PropertyValue<double>)value;
                    enumStruct.Value = pvalue.value;
                }
                list.Add(enumStruct);
            }

            return list;
        }

        /// <summary>
        /// 设备数据更新
        /// </summary>
        /// <param name="deviceData"></param>
        /// <param name="device"></param>
        public void UpdateDeviceData(AliIotDeviceData<ECU1151Info> deviceData, DeviceInfo device)
        {
            ECU1151Info deviceProperty = deviceData.items;
            List<EnumStruct> meterPropertyList = GetPropertyDatas(deviceProperty);
            foreach (var item in meterPropertyList)
            {
                if (string.IsNullOrEmpty(item.Description)) continue;
                var devData = deviceDataServices.QueryableToEntity(x => x.DeviceId == device.DeviceId && x.ParameterCode == item.Key);
                if (devData != null)
                {
                    if(item.Value !=null)
                    {
                        devData.ParameterValue = item.Value.ToString();
                    }
                    devData.LastTime = DateTime.Now;
                    deviceDataServices.UpdateEntity(devData);
                }
                else
                {
                    devData = new DeviceData();
                    devData.DeviceId = device.DeviceId;
                    devData.ParameterCode = item.Key;
                    devData.ParameterName = item.Description;
                    if (item.Value != null)
                    {
                        devData.ParameterValue = item.Value.ToString();
                    }
                    devData.ParameterStatus = 0;
                    devData.LastTime = DateTime.Now;
                    deviceDataServices.Insert(devData);
                }

                DeviceDataHistory deviceDataHistory = new DeviceDataHistory();//插入记录
                deviceDataHistory.DeviceId = device.DeviceId;
                deviceDataHistory.ParameterCode = item.Key;
                deviceDataHistory.ParameterName = item.Description;
                deviceDataHistory.ParameterStatus = 1;
                if (item.Value != null)
                {
                    deviceDataHistory.ParameterValue = item.Value.ToString();
                }
                deviceDataHistory.CreateTime = DateTime.Now;
                deviceDataHistoryServices.Insert(deviceDataHistory);
            }
        }
    }
}
